package com.fuxue.service.impl;

import com.fuxue.constant.MessageConstant;
import com.fuxue.constant.StatusConstant;
import com.fuxue.dto.*;
import com.fuxue.entity.AllFiled;
import com.fuxue.entity.Server;
import com.fuxue.entity.User;
import com.fuxue.exception.PasswordErrorException;
import com.fuxue.mapper.UserMapper;
import com.fuxue.service.UserService;
import com.fuxue.vo.ServerVO;
import com.fuxue.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import javax.security.auth.login.AccountLockedException;
import javax.security.auth.login.AccountNotFoundException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public User login(UserLoginDTO userLoginDTO)
    {
        String username = userLoginDTO.getUserName();
        String password = userLoginDTO.getPassword();

        //1、根据用户名查询数据库中的数据
        User user = userMapper.getByUsername(username);

        //2、处理各种异常情况（用户名不存在、密码不对、账号被锁定）
        if (user == null) {
            //账号不存在
            try {
                throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
            } catch (AccountNotFoundException e) {
                throw new RuntimeException(e);
            }
        }

        //密码比对
        //对前端传过来的明文密码进行md5加密处理
        //TODO 以后需要改造
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        System.out.println(password);
        if (!password.equals(user.getPassword())) {
            //密码错误
            System.out.println(user.getPassword());
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }

        if (user.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            try {
                throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
            } catch (AccountLockedException e) {
                throw new RuntimeException(e);
            }
        }

        return user;
    }

    @Override
    @Transactional
    public void save(UserDTO userDTO) {

        //对象属性拷贝
        //employee.setUpdateTime(LocalDateTime.now());
        String password = userDTO.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        userDTO.setPassword(password);
        userDTO.setCreatTime(LocalDateTime.now());
        userMapper.insert(userDTO);
      //  Integer id = userMapper.getIdByHao(userDTO.getHao());
       /* ServerUPFiledDTO serverUPFiledDTO = new ServerUPFiledDTO();
        serverUPFiledDTO.setServerId(id);
        serverUPFiledDTO.setFileds(serverDTO.getFileds());
        updateWithFiled(serverUPFiledDTO);*/
    }

    @Override
    public UserVO getById(Integer id) {
        User user=new User();
        user= userMapper.getById(id);
        UserVO userVO=new UserVO();
        BeanUtils.copyProperties(user,userVO);
        return userVO;
    }


    @Override
  public  void updateWithName(UserUpNameDTO userUpNameDTO)
    {
        userMapper.updateNameById(userUpNameDTO);
        userMapper.updateQuestionName(userUpNameDTO);

    }

    @Override
   public void updateWithPassword(UserUpPasswordDTO userUpPasswordDTO)
    {
        Integer id = userUpPasswordDTO.getUserId();
        String Oldpassword = userUpPasswordDTO.getOldpassword();
        String Newpassword = userUpPasswordDTO.getPassword();

        User user=userMapper.getUserById(id);
        if (user== null) {
            //账号不存在
            try {
                throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);
            } catch (AccountNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
        Oldpassword = DigestUtils.md5DigestAsHex(Oldpassword.getBytes());
        if (!Oldpassword.equals(user.getPassword())) {
            //密码错误
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        Newpassword = DigestUtils.md5DigestAsHex(Newpassword.getBytes());
        userMapper.UpdateNewPassword(id,Newpassword);


        if (user.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            try {
                throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
            } catch (AccountLockedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Override
  public  void updateWithAdvate(UserUpAdvateDTO userUpAdvateDTO)
  {

      userMapper.updateAdvatar(userUpAdvateDTO);
  }

    @Override
    public List<AllFiled> getAllFiled() {
        List<AllFiled>list = new ArrayList<AllFiled>();
        list = userMapper.selectAllFiled();
        return list;
    }
}
